package com.contory.ssn_lgnd.app.comp;

import com.contory.ssn_lgnd.app.Assets;
import com.contory.ssn_lgnd.framework.Input.TouchEvent;
import com.contory.ssn_lgnd.framework.Sound;
import com.contory.ssn_lgnd.framework.gl.SpriteBatcher;
import com.contory.ssn_lgnd.framework.gl.TextureRegion;
import com.contory.ssn_lgnd.framework.math.OverlapTester;
import com.contory.ssn_lgnd.framework.math.Rectangle;
import com.contory.ssn_lgnd.framework.math.Vector2;

public class ButtonObject extends ViewObject implements UpdatePtr, DrawPtr, TouchPtr{
	//public interface ButtonObjectTodo{
	//	public void todo();
	//}
	
	protected Sound sound = null;
	protected Rectangle bound = new Rectangle();
	protected TouchEvent touchEvent = null;
	protected Vector2 touchPoint = new Vector2();
	protected ButtonObjectPtr todo = null;
	protected TextureRegion tr_icon = null;
	protected float iconSize;
	
	protected String name = "";
	protected float nameSize;
	
	public boolean excuteBool = false;
	
	protected static final byte idle = 0;
	protected static final byte clicked = 1;
	protected static final byte excute = 2;
	protected byte state_ptr = 0;
	
	public void setIcon(TextureRegion tr_icon){
		this.tr_icon = tr_icon;
	}
	
	public void setTouchEvent(TouchEvent touchEvent, float touchX, float touchY){
		this.touchEvent = touchEvent;
		touchPoint.set(touchX, touchY);
	}
	
	public void setTodo(ButtonObjectPtr todo){
		this.todo = todo;
	}
	
	public void setSound(Sound sound){
		this.sound = sound;
	}
	
	@Override
	public void update(float deltaTime) {
		if(touchEvent != null){
			switch(state_ptr){
				case idle:
					if(touchEvent.type == TouchEvent.TOUCH_DOWN && !excuteBool){
						if(OverlapTester.pointInRectangle(bound, touchPoint)){
							r = g = b = a = 0.5f;
							state_ptr = clicked;
							if(sound != null)
								Assets.playSound(sound);
						}
					}
				break;
				case clicked:
					if(touchEvent.type == TouchEvent.TOUCH_UP){
						if(OverlapTester.pointInRectangle(bound, touchPoint)){
							state_ptr = excute;
						}
					}
					else if(touchEvent.type == TouchEvent.TOUCH_DRAGGED){
						if(!OverlapTester.pointInRectangle(bound, touchPoint)){
							r = g = b = a = 1.0f;
							state_ptr = idle;
						}
					}
				break;
				case excute:
					r = g = b = a = 1.0f;
					excuteBool = true;
					if(todo != null)
						todo.todo();
					state_ptr = idle;
				break;
			}
		}
	}

	@Override
	public void draw(SpriteBatcher batcher) {
		if(tr != null)
			batcher.drawSprite(x, y, w, h, r, g, b, a, tr);
		if(tr_icon != null)
			batcher.drawSprite(x, y, iconSize, iconSize, r, g, b, a, tr_icon);
		if(name != "")
			Assets.font.drawText(batcher, name, x, y, nameSize, nameSize, r, g, b, a);
	}
	
	public void setXY_WH_TR(float x, float y, float w, float h, TextureRegion tr){
		super.setXY_WH_TR(x, y, w, h, tr);
		bound.lowerLeft.set(x-w/2, y-h/2);
		bound.width = w;
		bound.height = h;
		nameSize = h * 0.8f;
		iconSize = h * 0.8f;
	}
	
	public void setString(String name){
		this.name = name;
	}
	
	public Rectangle getBound(){
		return bound;
	}
}
